package com.im.service.group.controller;

import com.im.common.model.RestResponse;
import com.im.common.model.SyncReq;
import com.im.common.model.SyncResp;
import com.im.service.group.entity.ImGroupEntity;
import com.im.service.group.model.req.*;
import com.im.service.group.model.resp.GetGroupResp;
import com.im.service.group.model.resp.GetJoinedGroupResp;
import com.im.service.group.service.GroupMessageService;
import com.im.service.group.service.ImGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/group")
@Api(tags = "群组控制器")
public class ImGroupController {

    @Autowired
    private ImGroupService groupService;
    @Autowired
    private GroupMessageService groupMessageService;

    @RequestMapping(value = "/importGroup", method = RequestMethod.POST)
    @ApiOperation(value = "导入群组", notes = "导入群组信息")
    public RestResponse importGroup(@RequestBody @Validated ImportGroupReq req, Integer appId, String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        groupService.importGroup(req);
        return RestResponse.success();
    }

    @RequestMapping(value = "/createGroup", method = RequestMethod.POST)
    @ApiOperation(value = "创建群组", notes = "创建群组")
    public RestResponse createGroup(@RequestBody @Validated CreateGroupReq req, Integer appId, String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        groupService.createGroup(req);
        return RestResponse.success();
    }

    @RequestMapping(value = "/getGroupInfo", method = RequestMethod.POST)
    @ApiOperation(value = "获取群组信息", notes = "获取群组信息")
    public RestResponse getGroupInfo(@RequestBody @Validated GetGroupReq req, Integer appId) {
        req.setAppId(appId);
        GetGroupResp group = groupService.getGroup(req);
        return RestResponse.success(group);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新群组信息", notes = "更新群组信息")
    public RestResponse update(@RequestBody @Validated UpdateGroupReq req, Integer appId, String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        groupService.updateBaseGroupInfo(req);
        return RestResponse.success();
    }

    @RequestMapping(value = "/getJoinedGroup",method = RequestMethod.POST)
    @ApiOperation(value = "获取加入的群组列表", notes = "获取加入的群组列表")
    public RestResponse getJoinedGroup(@RequestBody @Validated GetJoinedGroupReq req, Integer appId, String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        GetJoinedGroupResp joinedGroup = groupService.getJoinedGroup(req);
        return RestResponse.success(joinedGroup);
    }

    @RequestMapping(value = "/destroyGroup",method = RequestMethod.POST)
    @ApiOperation(value = "解散群组", notes = "解散群组")
    public RestResponse destroyGroup(@RequestBody @Validated DestroyGroupReq req, Integer appId, String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        groupService.destroyGroup(req);
        return RestResponse.success();
    }

    @RequestMapping(value = "/transferGroup",method = RequestMethod.POST)
    @ApiOperation(value = "转让群组", notes = "转让群组")
    public RestResponse transferGroup(@RequestBody @Validated TransferGroupReq req, Integer appId, String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        groupService.transferGroup(req);
        return RestResponse.success();
    }

    @RequestMapping(value = "/forbidSendMessage",method = RequestMethod.POST)
    @ApiOperation(value = "禁言群组", notes = "禁言群组")
    public RestResponse forbidSendMessage(@RequestBody @Validated MuteGroupReq req, Integer appId, String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        groupService.muteGroup(req);
        return RestResponse.success();
    }

    @RequestMapping(value = "/sendMessage",method = RequestMethod.POST)
    @ApiOperation(value = "发送群组消息", notes = "发送群组消息")
    public RestResponse sendMessage(@RequestBody @Validated SendGroupMessageReq req, Integer appId, String identifier) {
        req.setAppId(appId);
        req.setOperator(identifier);
        return RestResponse.success(groupMessageService.send(req));
    }

    @RequestMapping(value = "/syncJoinedGroup",method = RequestMethod.POST)
    @ApiOperation(value = "同步加入的群组列表", notes = "同步加入的群组列表")
    public RestResponse syncJoinedGroup(@RequestBody @Validated SyncReq req, Integer appId) {
        req.setAppId(appId);
        SyncResp<ImGroupEntity> imGroupEntitySyncResp = groupService.syncJoinedGroupList(req);
        return RestResponse.success(imGroupEntitySyncResp);
    }

}

